/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.role.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.role.entity.Role;
import com.application.role.struts.form.RoleForm;
import com.manager.BaseDao;
import com.manager.commons.DateUtil;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:RoleDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 左磊 QQ：30575951 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class RoleDAO extends BaseDao {
	private static final Logger logger = Logger.getLogger(RoleDAO.class);

	private Role role;

	public Role getRole() {
		return this.role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Integer getAllCountRole(RoleForm roleForm) {
		// 储存查询条件的buffer
		StringBuffer buf = new StringBuffer();
		buf.append("select count(*) from Role t ");
		// 查询字符串
		String strSql = getStringBuffer(roleForm, buf).toString();

		return Integer.parseInt(this.getHibernateTemplate().find(strSql).get(0)
				.toString());
	}

	public void deleteRoles(RoleForm roleForm) {
		String a = roleForm.getIds().replaceAll(",", "','");
		String hql = " delete from Role where id in  ( '" + a + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteRoles failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchRole(final RoleForm roleForm, final PageBean pageBean) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;

		buf.append("from Role t  ");

		// 查询字符串条件
		buf = getStringBuffer(roleForm, buf);

		// 传递查询条件buf值
		pageBean.setStringBuffer(buf);

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List resutlList = new ArrayList();
				try {
					Query query = session.createQuery(pageBean
							.getStringBuffer().toString()
							+ " order by t.createDate Desc ");

					// 设置开始页
					query.setFirstResult(pageBean.getPageSize()
							* (pageBean.getPageIndex() - 1));

					// 设置结束页
					query.setMaxResults(pageBean.getPageSize());

					List teamsList = query.list();
					if (teamsList != null && teamsList.size() > 0) {
						for (int j = 0; j < teamsList.size(); j++) {
							Role role = (Role) teamsList.get(j);
							resutlList.add(role);
						}
					}
					return resutlList;
				} catch (RuntimeException re) {
					logger.error("searchRole failed");
					throw re;
				} finally {
					if (session != null) {
						releaseSession(session);
					}
				}
			}
		});
		return list;
	}

	public List searchRole(final RoleForm roleForm) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;

		buf.append("from Role t  ");

		// 查询字符串条件
		buf = getStringBuffer(roleForm, buf);
		final String sql = buf.toString();

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List resutlList = new ArrayList();
				try {
					Query query = session.createQuery(sql
							+ " order by t.createDate Desc ");

					List teamsList = query.list();
					if (teamsList != null && teamsList.size() > 0) {
						for (int j = 0; j < teamsList.size(); j++) {
							Role role = (Role) teamsList.get(j);
							resutlList.add(role);
						}
					}
					return resutlList;
				} catch (RuntimeException re) {
					logger.error("searchRole failed");
					throw re;
				} finally {
					if (session != null) {
						releaseSession(session);
					}
				}
			}
		});
		return list;
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param roleForm
	 * @return
	 */
	private StringBuffer getStringBuffer(RoleForm roleForm, StringBuffer buf) {
		/*
		 * 需要加入查询条件时封装
		 */
		buf.append(" where 1=1 ");
		if (StringUtils.isNotBlank(roleForm.getName())) {
			buf.append(" and t.name = '" + roleForm.getName().trim() + "'");
		}
		if (StringUtils.isNotBlank(roleForm.getId())) {
			buf.append(" and t.id = '" + roleForm.getId().trim() + "'");
		}
		return buf;
	}
}
